package com.third.login.umeng;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.dg.acb.utils.configdata.XXConstants;
import com.third.login.ThirdLoginBase;
import com.third.login.ThirdLoginData;
import com.third.login.ThirdLoginPlatform;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

/**
 * Created by fzh on 2017/8/2.
 */

public class UmengThirdLoginBase extends ThirdLoginBase {
    protected SHARE_MEDIA mSHARE_MEDIA;
    protected UMAuthListener authListener;
    protected  ThirdLoginPlatform mThirdLoginPlatform;
    public static final String unionidKey = "unionid";
    public static final String appidKey = "appid";
    public static final String accessTokenKey = "accessToken" ;
    public static final String access_TokenKey = "access_token" ;
    public static final String authTimeKey = "auth_time";
    public static final String pfKey = "pf";
    public static final String payTokenKey = "pay_token" ;
    public static final String openIdKey = "openid";
    public static final String pageTypeKey = "page_type";
    public static final String pfKeyKey = "pfkey" ;
    public static final String uidKey ="uid";
    public static final String sendInstallKey = "sendinstall";
    public static final String expiresInKey ="expires_in";
    public static final String retKey = "ret";
    public static final String access_token="access_token";
    public UmengThirdLoginBase(Activity activity,String appKey,String appId){
            super(activity,appKey,appId);
            initUMAuthListener();
            init();
    }

    protected  void init(){
        mSHARE_MEDIA = SHARE_MEDIA.QQ;
        mThirdLoginPlatform = ThirdLoginPlatform.QQ;
        PlatformConfig.setQQZone(XXConstants.appKeyQQ, XXConstants.appSecretQQ);
    }

    @Override
    public void thirdLogin() {
        UMShareAPI.get(act).getPlatformInfo(act,mSHARE_MEDIA, authListener);
        //UMShareAPI.get(act).doOauthVerify(act,mSHARE_MEDIA, authListener);
    }

    @Override
    public void thirdLogout() {
        //UMShareAPI.get(act).getPlatformInfo(act, mSHARE_MEDIA, authListener);
        UMShareAPI.get(act).deleteOauth(act, mSHARE_MEDIA, authListener);
    }

    @Override
    public void cancelLogin() {
        UMShareAPI.get(act).deleteOauth(act, mSHARE_MEDIA, authListener);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(act).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        UMShareAPI.get(act).release();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        UMShareAPI.get(act).onSaveInstanceState(outState);
    }

    private void initUMAuthListener(){
            authListener = new UMAuthListener(){

            @Override
            public void onStart(SHARE_MEDIA share_media) {
                if(thirdLoginAuthListener != null){
                    thirdLoginAuthListener.onStart(mThirdLoginPlatform);
                }
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                if(thirdLoginAuthListener != null){
                    ThirdLoginData data = getThirdData(map);
                    thirdLoginAuthListener.onComplete(mThirdLoginPlatform,i,data);
                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                if(thirdLoginAuthListener != null){
                    thirdLoginAuthListener.onError(mThirdLoginPlatform,i,"",throwable);
                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                if(thirdLoginAuthListener != null){
                    thirdLoginAuthListener.onCancel(mThirdLoginPlatform,i);
                }
            }
        };
    }

    public static ThirdLoginData getThirdData(Map<String,String> map){
        Log.i("drj", map.toString());
        ThirdLoginData thirdLoginData = new ThirdLoginData();
        thirdLoginData.access_Token = map.get(access_TokenKey);
        thirdLoginData.accessToken = map.get(accessTokenKey);
        thirdLoginData.unionid = map.get(unionidKey);
        thirdLoginData.appid = map.get(appidKey);
        thirdLoginData.auth_time = map.get(authTimeKey);
        thirdLoginData.pf = map.get(pfKey);
        thirdLoginData.pay_token = map.get(payTokenKey);
        thirdLoginData.openid = map.get(openIdKey);
        thirdLoginData.page_type = map.get(pageTypeKey);
        thirdLoginData.pfkey = map.get(pfKeyKey);
        thirdLoginData.uid = map.get(uidKey);
        thirdLoginData.accessToken = map.get(accessTokenKey);
        thirdLoginData.sendinstall = map.get(sendInstallKey);
        thirdLoginData.expires_in = map.get(expiresInKey);
        thirdLoginData.ret = map.get(retKey);
        thirdLoginData.data = map;
        return thirdLoginData;
    }


}
